package com.networklib.http.impl.post;

import com.networklib.http.exception.NetException;

import java.io.File;
import java.util.Map;
import java.util.Set;

import okhttp3.MultipartBody;
import okhttp3.RequestBody;

public class MutiPostHttpRequestImpl extends PostHttpRequestImpl {
    private final Map<String, Object> params;

    public MutiPostHttpRequestImpl(String url, Map<String, Object> params) throws NetException {
        super(url);
        this.params = params;
    }


    @Override
    protected RequestBody getParams() {
        MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);
        Set<Map.Entry<String, Object>> entries = params.entrySet();
        for (Map.Entry<String, Object> entry : entries) {
            Object value = entry.getValue();
            if (value instanceof File) {
                builder.addFormDataPart(entry.getKey(), ((File) value).getName(), getRequestBody(value, onProgressListener));
            } else {
                builder.addFormDataPart(entry.getKey(), (String) value);
            }
        }
        return builder.build();
    }
}
